package com.jack.leetcode.每日一题;

public class 验证回文串 {

    public static void main(String[] args) {
        System.out.println(isPalindrome("race a car"));
    }

    public static boolean isPalindrome(String s) {
        int n = s.length();
        if(n == 0){
            return true;
        }
        int left = 0, right = n-1;
        while(left < right){
            while(left < n && (!Character.isDigit(s.charAt(left)) && !Character.isLetter(s.charAt(left)))){
                left++;
            }
            while(right >=0 && (!Character.isDigit(s.charAt(right)) && !Character.isLetter(s.charAt(right)))){
                right--;
            }
            if(left >= n){
                break;
            }
            if(right < 0){
                break;
            }
            char leftChar = Character.toLowerCase(s.charAt(left));
            char rightChar = Character.toLowerCase(s.charAt(right));
            if(leftChar != rightChar) {
                return false;
            }else{
                left++;
                right--;
            }
        }
        return true;
    }
}
